package com.bbu.javaexam.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bbu.javaexam.domain.FillQuestion;
import com.bbu.javaexam.domain.JudgeQuestion;
import com.bbu.javaexam.domain.MultiQuestion;
import com.bbu.javaexam.domain.RespData;
import com.bbu.javaexam.service.FillQuestionService;
import com.bbu.javaexam.service.JudgeQuestionService;
import com.bbu.javaexam.service.MultiQuestionService;
import com.bbu.javaexam.service.SelectQuestionsService;
import com.bbu.javaexam.util.RespDataHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author Alex
 * @version 1.0.0
 * @ClassName SelectQuestionsController.java
 * @Description TODO
 * @createTime 2022年02月23日 10:20:00
 */
@RestController
@RequestMapping("/selectquestion")
public class SelectQuestionsController {

    @Autowired
    private SelectQuestionsService selectQuestionsService;

    @Autowired
    private MultiQuestionService multiQuestionService;

    @Autowired
    private JudgeQuestionService judgeQuestionService;

    @Autowired
    private FillQuestionService fillQuestionService;

    @GetMapping("/{questionId}/{type}")
    public RespData<Object> selectQuestions(@PathVariable("questionId") Integer questionId, @PathVariable("type") String type) {
        Object question = selectQuestionsService.selectQuestion(questionId, type);
        return RespDataHandler.success(question);
    }

    @PostMapping("/updateQ")
    public RespData<Object> updateQ(@RequestBody Map map) {
        if (!ObjectUtils.isEmpty(map.get("answerA"))) {
            MultiQuestion m = new MultiQuestion();
            m.setAnalysis((String) map.get("analysis"));
            m.setQuestion((String) map.get("question"));
            m.setAnswerA((String) map.get("answerA"));
            m.setAnswerB((String) map.get("answerB"));
            m.setAnswerC((String) map.get("answerC"));
            m.setAnswerD((String) map.get("answerD"));
            m.setLevel((String) map.get("level"));
            m.setQuestionId((Integer) map.get("questionId"));
            m.setRightAnswer((String) map.get("rightAnswer"));
            m.setScore((Integer) map.get("score"));
            m.setSubject((String) map.get("subject"));
            m.setSection((String) map.get("section"));
            multiQuestionService.updateById(m);
            return RespDataHandler.success(null);
        }
        JudgeQuestion j = new JudgeQuestion();
        j.setAnalysis((String) map.get("analysis"));
        j.setQuestion((String) map.get("question"));
        j.setLevel((String) map.get("level"));
        j.setQuestionId((Integer) map.get("questionId"));
        j.setScore((Integer) map.get("score"));
        j.setSubject((String) map.get("subject"));
        j.setSection((String) map.get("section"));
        j.setAnswer((String) map.get("answer"));
        LambdaQueryWrapper<JudgeQuestion> lam = new LambdaQueryWrapper<>();
        lam.eq(JudgeQuestion::getQuestionId, j.getQuestionId());
        lam.eq(JudgeQuestion::getQuestion, j.getQuestion());
        JudgeQuestion judgeQuestion = judgeQuestionService.getOne(lam);
        if (ObjectUtils.isEmpty(judgeQuestion)) {
            FillQuestion fillQuestion = new FillQuestion();
            fillQuestion.setAnalysis((String) map.get("analysis"));
            fillQuestion.setQuestion((String) map.get("question"));
            fillQuestion.setLevel((String) map.get("level"));
            fillQuestion.setQuestionId((Integer) map.get("questionId"));
            fillQuestion.setScore((Integer) map.get("score"));
            fillQuestion.setSubject((String) map.get("subject"));
            fillQuestion.setSection((String) map.get("section"));
            fillQuestion.setAnswer((String) map.get("answer"));
            fillQuestionService.updateById(fillQuestion);
            return RespDataHandler.success(null);
        }
        judgeQuestionService.updateById(j);
        return RespDataHandler.success(null);
    }

}
